package com.itheima.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.base.Area;
import com.itheima.bos.service.base.AreaService;
import com.itheima.bos.utils.PinYin4jUtils;
import com.itheima.bos.web.action.common.BaseAction;

@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class AreaAction extends BaseAction<Area> {

	@Autowired
	private AreaService areaService;

	private File file;

	public void setFile(File file) {
		this.file = file;
	}

	/*
	 * 导入exl表并保存到数据库中
	 */
	@Action("area_batchImport")
	public String batchImport() throws FileNotFoundException, IOException {

		List<Area> list = new ArrayList<Area>();

		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
		for (Row row : hssfSheet) {
			if (row.getRowNum() == 0) {
				continue;
			}
			if (row.getCell(0) == null && StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}

			// 封装数据
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());

			// 基于Pinyin4j生成城市编码和简码
			String province = area.getProvince();
			String city = area.getCity();
			String districe = area.getDistrict();
			// 截取省市区字符
			String provinceStr = province.substring(0, province.length() - 1);
			String cityStr = city.substring(0, city.length() - 1);
			String districeStr = districe.substring(0, districe.length() - 1);

			// 生成简码
			String[] headArray = PinYin4jUtils.getHeadByString(provinceStr + cityStr + districeStr);
			StringBuilder sb = new StringBuilder();
			for (String str : headArray) {
				sb.append(str);
			}
			String shortcode = sb.toString();
			area.setShortcode(shortcode);
			// 生成城市编码
			String citycode = PinYin4jUtils.hanziToPinyin(cityStr);
			area.setCitycode(citycode);

			list.add(area);
		}

		areaService.saveBatch(list);

		return NONE;
	}

	/*
	 * 按条件查询区域的信息
	 */


	@Action(value = "area_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {

		Pageable pageable = new PageRequest(page - 1, rows);
		Specification<Area> spec = new Specification<Area>() {

			List<Predicate> list = new ArrayList<Predicate>();

			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province").as(String.class), "%" + model.getProvince() + "%");
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p1 = cb.like(root.get("city").as(String.class), "%" + model.getCity() + "%");
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p1 = cb.like(root.get("district").as(String.class), "%" + model.getDistrict() + "%");
					list.add(p1);
				}

				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Area> pageData = areaService.findPageData(spec, pageable);
		
		pushPageDataToValueStack(pageData);
		
		return SUCCESS;
	}
}
